package com.ihealthway.all.interfaces.dwihealth.modules;

import com.ihealthway.all.interfaces.dwihealth.vo.DWCustSerItem;
import com.ihealthway.all.interfaces.dwihealth.vo.DWCustomer;
import com.ihealthway.all.interfaces.dwihealth.vo.DWSerPlanItem;
import com.ihealthway.all.interfaces.dwihealth.vo.DWServicePlan;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/dw_inter/query")
public class DWQueryDataModule {

    private static final Log logger = Logs.get();

    @Inject
    private NutDao dao;

    @At
    @Ok("beetl:dw/dw_query_data.jsp")
    public void home() {

    }

    @At
    public Object customerList(Integer page, Integer rows, @Param("no") String no) {
        return listResult(page, rows, no, DWCustomer.class, "cust_no");
    }

    @At
    public Object custSerPItemList(Integer page, Integer rows, @Param("no") String no) {
        return listResult(page, rows, no, DWCustSerItem.class, "cust_no");
    }

    @At
    public Object servicePlanList(Integer page, Integer rows, @Param("no") String no) {
        return listResult(page, rows, no, DWServicePlan.class, "serplan_id");
    }

    @At
    public Object serPlanItemList(Integer page, Integer rows, @Param("no") String no) {
        return listResult(page, rows, no, DWSerPlanItem.class, "serplan_id");
    }


    private Map<String, Object> listResult(Integer page, Integer rows, String no, Class<?> clazz, String columnName) {
        Map<String, Object> result = new HashMap<>();
        Criteria cri = Cnd.cri();
        if (Strings.isNotBlank(no)) {
            cri.where().andEquals(columnName, no);
        }

        result.putAll(easyuiDGPager(clazz, page, rows, cri));

        return result;
    }


    private Map<String, Object> easyuiDGPager(Class<?> clazz, Integer curPage, Integer pageSize, Criteria cri) {

        Map<String, Object> map = new HashMap<>();
        List<?> data = null;

        if (cri == null) {
            cri = Cnd.cri();
        }

        cri.where().and("data_status", "=", "1");
        Pager pager = null;
        if (curPage>0 && pageSize>0) {
            pager = this.dao.createPager(curPage, pageSize);
        }

        try {
            data = this.dao.query(clazz, cri, pager);
            map.put("rows", data);
        } catch (Exception e) {
            logger.errorf("%s 创建DataGrid分页数据异常!!!条件: %s", clazz.getSimpleName(),
                    cri.toString());
            logger.error(e);
        }

        try {
            map.put("total", this.dao.count(clazz, cri));
        } catch (Exception e) {
            logger.errorf("%s 统计总记录数据时异常!!!", clazz.getSimpleName());
            logger.error(e);
        }

        return map;
    }
}
